#ifndef FM_TEST_SOCKET_CLIENT_H
#define FM_TEST_SOCKET_CLIENT_H


#include "../../src/Stream.h"
#include "../plc_rte/rtos.h"

#ifdef windows_x86
#include <ws2tcpip.h>
#define MSG_DONTWAIT 0x0
#endif
#include<vector>
class socket_client : public Stream {

public:
    virtual int begin(const char *host, uint16_t port);

    virtual size_t write(uint8_t);

    virtual size_t write(const uint8_t *buf, size_t size);

    int available() override;

    int read() override;

    virtual int read(uint8_t *buf, size_t size);

    int peek() override;

    void flush() override;

    virtual void stop();

    virtual uint8_t connected();

    virtual explicit operator bool();

    void loop();

    static void thd_loop(void *arg) {
        auto client = (socket_client *) arg;
        client->loop();
    };
    int sock;
    struct sockaddr_in client_addr;
    int PORT;
    const char *IP_ADDR;
    uint8_t *rxbuf;
    std::vector<uint8_t> txbuf;
    uint16_t rx_head, rx_tail;
    uint16_t rx_size = 1024;
};


#endif //FM_TEST_SOCKET_CLIENT_H
